﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web.SessionState;

using LDAP.Utility;
using LDAP.Web;
using LDAP.Entity;
using LDAP.BLL;

namespace LDAP.Handler
{
    public class AttendanceControl : BaseWebHandler, IRequiresSessionState, IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action) {
                case "getAngels":
                    GetAngels();
                    break;
                case "submitExceptionForm":
                    SubmitExceptionForm();
                    break;
                case "delExceptionForm":
                    DelExceptionForm();
                    break;
                case "getAttachementInfo":
                    GetAttachementInfo();
                    break;
                default:
                    Response.Write("hello world!");
                    break;
            }

            Response.End();
        }

        public void GetAngels()
        {
            Response.Write(ToJson(BizAngel.GetAll()));
        }

        public void DelExceptionForm() {
            int id = Tools.Web.Request.GetFormInt("id", 0);

            JsonTransfer jt = new JsonTransfer();
            Exceptionform ef = BizExceptionform.GetByKey(id);
            if (ef == null || ef.UserId!= CurrentUser.Id)
            {
                jt.SetApiFailed();
                Response.Write(jt);
                return;
            }

            if (ef.Status != 0)
            {
                jt.SetApiFailed("审批过的单据不允许作废");
                Response.Write(jt);
                return;
            }

            BizAttachment.Delete(ef.Attachement);
            BizExceptionform.Delete(id);

            jt.SetApiSuccess();
            Response.Write(jt);
        }

        public void SubmitExceptionForm()
        {
            string startTime = Tools.Web.Request.GetFormString("startTime");
            string endTime = Tools.Web.Request.GetFormString("endTime");
            string duration = Tools.Web.Request.GetFormString("duration");
            int type = Tools.Web.Request.GetFormInt("type", 0);
            string ids = Tools.Web.Request.GetFormString("ids");
            
            JsonTransfer jt = new JsonTransfer();
            Exceptionform ef = null;
            try
            {
                ef = new Exceptionform
                {
                    UserId = CurrentUser.Id,
                    Dept = ";" + CurrentUser.DeptName.Replace("17173-","").Trim(new[] { ' ', ';' }) + ";",
                    CreateTime = DateTime.Now,
                    ExceptionStartDate = Convert.ToDateTime(startTime),
                    ExceptionEndDate = string.IsNullOrEmpty(endTime) ? Convert.ToDateTime(startTime) : Convert.ToDateTime(endTime),
                    Type = type,
                    //DurationType = string.IsNullOrEmpty(endTime) ? Convert.ToInt32(duration) : 0,
                    Duration = !string.IsNullOrEmpty(duration) ? (float)Convert.ToDouble(duration) : 0.0f,
                    Content = string.Empty,
                    Attachement = ids.Trim(new[] { ' ', ',' }),
                    Status = 0
                };
            }
            catch (Exception ex) {
                ef = null;
                jt.SetError(ex.Message);
                jt.SetApiFailed("提交数据有问题，不要乱来哦，我在背后看着你");
            }

            if (ef == null) {
                Response.Write(jt);
                Response.End();
                return;
            }

            if (BizExceptionform.Add(ef) > 0)
            {
                jt.Add("ef", ef);
                jt.SetApiSuccess();
            }
            else
                jt.SetApiFailed();

            Response.Write(jt);
        }

        public void GetAttachementInfo() {
            string ids = Tools.Web.Request.GetQueryString("id");
            JsonTransfer jt = new JsonTransfer();
            jt.SetApiSuccess();
            jt.Add("list", BizAttachment.GetListByIds(ids));
            Response.Write(jt);
        }
    }
}